<?php

namespace App\Http\Controllers;

use App\Models\Product;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;

class ProductController extends Controller
{
    private $URL;

    public function __construct()
    {
        $this->URL = env('QINIUURL');
    }


    /**
     * 所有model
     * Author: 曾煌
     * WeChat: Big杰
     * Date: 2019/6/4
     * Time: 18:25
     *
     * @param Channel $channel
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function allProduct()
    {
        $url = $this->URL;
        $seo = Redis::get('seo');

        $allModel          = Redis::get('Info');
        $data['all_model'] = json_decode($allModel, true);

        $data['seo'] = json_decode($seo, true);
        $data['url'] = $url;

        $where = [
            'is_release' => 1,
        ];

        $data['product'] = Product::query()
                                  ->with('product_auxiliaries')
                                  ->where($where)
                                  ->orderByDesc('sort')
                                  ->orderBy('id', 'desc')
                                  ->select('title', 'image', 'seo_title', 'seo_description', 'seo_keyword', 'id')
                                  ->paginate();

        $data['text'] = '模特列表';

        return view('model.model', $data);
    }

    /**
     * 产品详情
     * Author: 曾煌
     * WeChat: Big杰
     * Date: 2019/6/4
     * Time: 17:27
     *
     * @param Request $request
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|\Illuminate\View\View
     */
    public function getProductDesc($id)
    {
        $result['data'] = Product::query()
                                 ->with('product_auxiliaries', 'product_photos', 'product_images')
                                 ->where('id', $id)
                                 ->where([
                                     'is_release' => 1,
                                 ])
                                 ->first(['title', 'image', 'seo_title', 'seo_description', 'seo_keyword', 'id']);

        if ($result) {
            Product::where('id', $id)->increment('pv');

            $result['url']       = $this->URL;
            $allModel            = Redis::get('Info');
            $result['all_model'] = json_decode($allModel, true);
            $result['text']      = '模特详情';

            $seo = Redis::get('seo');
            $result['seo'] = json_decode($seo, true);

            return view('model.modelDetails', $result);
        } else {
            return redirect('/');
        }
    }
}
